﻿;EnableExplicit

IncludePath "Source/"
	IncludeFile "threeD.pb"
IncludePath "/"

Structure SimplestVertex
	pos.vec3
	texCoord.vec2
EndStructure

Procedure SetVertex(*vertex.SimplestVertex, x.f,y.f,z.f, s.f,t.f)
	*vertex\pos\x = x	: *vertex\pos\y = y :	*vertex\pos\z = z
	*vertex\texCoord\x = s	: *vertex\texCoord\y = t
EndProcedure


Procedure.l CreateFullscreenQuad()
	Protected idVAO.l
	Protected idVBO.l
	
	; vertices in screen space
	Protected Dim vertexData.SimplestVertex(6)
	SetVertex(@vertexData(0), -1.0, -1.0, 0.0,   0.0, 0.0)
	SetVertex(@vertexData(1),  1.0, -1.0, 0.0,   1.0, 0.0)
	SetVertex(@vertexData(2), -1.0,  1.0, 0.0,   0.0, 1.0)
	SetVertex(@vertexData(3),  1.0, -1.0, 0.0,   1.0, 0.0)
	SetVertex(@vertexData(4),  1.0,  1.0, 0.0,   1.0, 1.0)
	SetVertex(@vertexData(5), -1.0,  1.0, 0.0,   0.0, 1.0)
	
	; create empty VAO and VBO
	glGenVertexArrays(1, @idVAO)
	glBindVertexArray(idVAO)
	glGenBuffers(1, @idVBO)
	
	; fill VBO by our vertices
	glBindBuffer(#GL_ARRAY_BUFFER, idVBO)
	glBufferData(#GL_ARRAY_BUFFER, 6 * SizeOf(SimplestVertex), @vertexData(), #GL_STATIC_DRAW)
	
	;setup vertex attributes in VBO
	glVertexAttribPointer(#LAYOUT_VERTEX_POS, 3, #GL_FLOAT, #GL_FALSE, SizeOf(SimplestVertex), OffsetOf(SimplestVertex\pos));
	glEnableVertexAttribArray(#LAYOUT_VERTEX_POS);
	
	glVertexAttribPointer(#LAYOUT_VERTEX_TEXCOORD, 2, #GL_FLOAT, #GL_FALSE, SizeOf(SimplestVertex), OffsetOf(SimplestVertex\texCoord));
	glEnableVertexAttribArray(#LAYOUT_VERTEX_TEXCOORD);
	
	ProcedureReturn idVAO
EndProcedure

Procedure RenderScene()
	
EndProcedure


UsePNGImageDecoder() 

If OpenWindow(0, 100, 200, 640, 480, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
  hDC = CreateOpenGLContext(WindowID(0))
  
  textureID = OpenGL_ImageToTexture(LoadImage(#PB_Any, "Data/skin.png"))
  shaderID = LoadShadersFromFiles("Data/testshader.vs", "Data/testshader.fs")
  fsQuadID = CreateFullscreenQuad()
  
  Repeat
  	Event = WaitWindowEvent(5)
    Select Event
    	Case #PB_Event_CloseWindow	
    		Quit = 1
    	Case #PB_Event_SizeWindow
    		glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
    EndSelect
    
    glClearColor_(0.3,0.3,0.3,1)
		glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
		
		;Rendering
		
		;Bind first texture
		glActiveTexture(#GL_TEXTURE0)
		glBindTexture_(#GL_TEXTURE_2D, textureID)
		
		;Bind shader program
		glUseProgram(shaderID)
		
		;Set texture uniform which will be used in shader
		uniformLocation = glGetUniformLocation(shaderID, @"u_BaseTexture")
		If uniformLocation <> -1
			glUniform1i(uniformLocation, 0) ; u_BaseTexture = #GL_TEXTURE0
		EndIf
		
		;Let's try to set time uniform in shader?
		uniformLocation = glGetUniformLocation(shaderID, @"u_Phase")
		If uniformLocation <> -1
			glUniform1f(uniformLocation, GetTickCount_()*0.001)
		EndIf
		
		;Draw fullscreen quad with tricky shader
		glBindVertexArray(fsQuadID)
		glDrawArrays_(#GL_TRIANGLES, 0, 6);6 vertices, start from zero element
		
		
    SwapBuffers_(hDC)
  Until Quit = 1
EndIf
;/// azaza

; IDE Options = PureBasic 5.11 (Windows - x86)
; CursorPosition = 4
; Folding = --
; EnableXP
; EnableCompileCount = 1
; EnableBuildCount = 0
; EnableExeConstant